<?php
	define('CONFIG_FILE',true);
    include '../config.php';
    
    define('DB_FILE',true);
    require_once '../Tool/DB/SqlDBManager.class.php';
    require_once '../Tool/Common/PrepareInput.php';
   
    session_start();
    if (empty($_SESSION["userType"])) {
        $url="../Index.php";
        include_once '../Tool/Common/Redirect.php';
        exit();
    }

    if($_SESSION["userType"]!="System Admin"){
        $url="../Index.php";
        include_once '../Tool/Common/Redirect.php';
        exit();
    }
    
    $InsertCentreName=$InsertAddress=$InsertPostalCode="";  

    if(isset($_REQUEST['InsertCentreName'])){ 
        $InsertCentreName=prepareInput($_REQUEST['InsertCentreName']); 
    }
    if(isset($_REQUEST['InsertAddress'])) { 
        $InsertAddress=prepareInput($_REQUEST['InsertAddress']); 
    }
    if(isset($_REQUEST['InsertPostalCode'])) { 
        $InsertPostalCode=prepareInput($_REQUEST['InsertPostalCode']); 
    }

    $result=array();

    $result['flagCentreName']=0;
    $result['flagAddress']=0;  
    $result['flagPostalCode']=0;
    $result['flagCentreNameExist']=1;

    //Check centrename exist or not
    $sqlDBManager = new SqlDBManager();
    $sql="Select CentreName FROM Centre";
    $res=$sqlDBManager->queryRows($sql);
    if(!empty($res)){
        for($i=0;$i<count($res);$i++){
            $result['flagCentreNameExist']=0;
            if($res[$i]['CentreName']==$InsertCentreName){
                break;
            }else{
                $result['flagCentreNameExist']=1; 
            }
        }
    }  

    if(preg_match("/^[A-Za-z0-9() ]*$/",$InsertCentreName)&&!empty($InsertCentreName)){
        $result['flagCentreName']=1;
    }
    if(preg_match("/^[0-9A-Za-z#-:() ]*$/",$InsertAddress)&&!empty($InsertAddress)){
        $result['flagAddress']=1;
    }
    if(preg_match("/^[0-9]{6}$/",$InsertPostalCode)&&!empty($InsertPostalCode)){
        $result['flagPostalCode']=1;
    }

    echo json_encode($result);
?>